package org.sgx.gwteditors.client.impl1.misc;

import java.util.List;

import org.sgx.gwteditors.client.editor.ConfigurableEditor;
import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.util.JsObject;

import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * an editor for navigatable urls that also shows a link.
 * 
 * @author sg
 * 
 */
public class LinkEditor extends VerticalPanel implements Editor<String>, ValueChangeHandler<String>, ConfigurableEditor {

	private TextBox textBox;
	private Anchor anchor;
	boolean editable;

	@Override
	public boolean isSimple() {
		return true;
	}

	public LinkEditor() {
		super();
		textBox = new TextBox();
		add(textBox);
		anchor = new Anchor();
		add(anchor);
		anchor.setText("link->");
		textBox.addValueChangeHandler(this);
	}

	@Override
	public String flush() {
		return textBox.getText();
		// no validation / errors
	}

	@Override
	public void load(String t) {
		if (!isEditable())
			textBox.setVisible(false);
		else
			textBox.setVisible(true);
		textBox.setText(t);
		anchor.setHref(t);
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().addValueChangeListener(this, l);
		anchor.setHref(textBox.getText());
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().removeValueChangeListener(this, l);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		EditorEventManager.<String> getInstance().fireValueChangeGwtEvent(this, event);
	}

	@Override
	public List<EditorError<String>> getErrors() {
		return EditorErrorManager.<String> getInstance().getErrors(this);
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[] { EditorFramework1.TYPE_STRING };
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	@Override
	public void config(String configStr) {
		JsObject jso = JsonUtils.unsafeEval("{" + configStr + "}");
		if (jso.objGetBoolean("editable"))
			editable = true;
		else
			editable = false;
		if (jso.objGetString("linkText") != null)
			anchor.setText(jso.objGetString("linkText"));
	}

}
